package com.example.asus.a17376037_zq_market;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import Info.Product;

import static com.example.asus.a17376037_zq_market.R.drawable.cup;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private RecyclerView recyclerView;
    private List<Product> productList = new ArrayList<>();
    private ProductAdapter adapter;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initNavigation();
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


        productList.add(new Product("鞋子",R.drawable.shoes));
        productList.add(new Product("耳机" ,R.drawable.earphones));
        productList.add(new Product("杯子", R.drawable.cup));
        productList.add(new Product("书包" ,R.drawable.bag));

        adapter =new ProductAdapter(productList);
        recyclerView.setAdapter(adapter);
        this.setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.menu);
        }
        mNavigationView.setItemIconTintList(null);
    }


    private void initNavigation() {
        mNavigationView = findViewById(R.id.nav_view);
        //设置menu的监听事件
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.user:
                        //启动
                        Intent loginIntent =new Intent(MainActivity.this, LonginActivity.class);
                        startActivity(loginIntent);
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.setting:
                        Intent SettingIntent = new Intent(MainActivity.this, SettingActivity.class);
                        startActivity(SettingIntent);
                        mDrawerLayout.closeDrawers();
                        break;
//                    case R.id.cart:
//                        Intent aboutIntent = new Intent(MainActivity.this, CartActivity.class);
//                        startActivity(aboutIntent);
//                        mDrawerLayout.closeDrawers();
//                        break;
//                    case R.id.release:
//                        Intent aboutIntent = new Intent(MainActivity.this, ReleaseActivity.class);
//                        startActivity(aboutIntent);
//                        mDrawerLayout.closeDrawers();
//                        break;
                    case R.id.cancellation:
                        mDrawerLayout.closeDrawers();
                        break;
                }
                return false;
            }
        });
    }
    private void init() {
        toolbar = findViewById(R.id.toolbar);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        recyclerView = findViewById(R.id.recycler_view);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            default:
        }
        return true;
    }
}
